import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

/// 支付成功页面
class OrderSuccessPage extends StatelessWidget {
  final String orderNo;
  final double amount;
  final Map<String, dynamic>? virtualContent;

  const OrderSuccessPage({
    super.key,
    required this.orderNo,
    required this.amount,
    this.virtualContent,
  });

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('支付成功'),
        backgroundColor: Colors.white,
        foregroundColor: Colors.black,
        elevation: 0,
        leading: IconButton(
          icon: const Icon(Icons.close),
          onPressed: () => Navigator.pop(context, {'success': true}),
        ),
      ),
      body: SingleChildScrollView(
        child: Column(
          children: [
            const SizedBox(height: 40),
            
            // 成功图标
            Container(
              width: 80,
              height: 80,
              decoration: const BoxDecoration(
                color: Colors.green,
                shape: BoxShape.circle,
              ),
              child: const Icon(
                Icons.check,
                color: Colors.white,
                size: 50,
              ),
            ),
            
            const SizedBox(height: 20),
            
            const Text(
              '支付成功',
              style: TextStyle(
                fontSize: 24,
                fontWeight: FontWeight.bold,
              ),
            ),
            
            const SizedBox(height: 8),
            
            Text(
              '¥${amount.toStringAsFixed(2)}',
              style: const TextStyle(
                fontSize: 32,
                fontWeight: FontWeight.bold,
                color: Colors.red,
              ),
            ),
            
            const SizedBox(height: 8),
            
            Text(
              '订单号：$orderNo',
              style: const TextStyle(
                fontSize: 12,
                color: Colors.grey,
              ),
            ),
            
            const SizedBox(height: 32),
            
            // 虚拟内容
            if (virtualContent != null) ...[
              Container(
                margin: const EdgeInsets.symmetric(horizontal: 16),
                padding: const EdgeInsets.all(16),
                decoration: BoxDecoration(
                  color: Colors.blue.shade50,
                  borderRadius: BorderRadius.circular(12),
                  border: Border.all(color: Colors.blue.shade200),
                ),
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    const Row(
                      children: [
                        Icon(Icons.download, color: Colors.blue),
                        SizedBox(width: 8),
                        Text(
                          '商品内容',
                          style: TextStyle(
                            fontSize: 18,
                            fontWeight: FontWeight.bold,
                          ),
                        ),
                      ],
                    ),
                    const SizedBox(height: 16),
                    
                    if (virtualContent!['downloadUrl'] != null) ...[
                      _buildInfoRow(
                        '下载地址',
                        virtualContent!['downloadUrl'],
                        onCopy: () => _copyToClipboard(context, virtualContent!['downloadUrl']),
                      ),
                      const SizedBox(height: 12),
                    ],
                    
                    if (virtualContent!['accessCode'] != null) ...[
                      _buildInfoRow(
                        '提取码',
                        virtualContent!['accessCode'],
                        onCopy: () => _copyToClipboard(context, virtualContent!['accessCode']),
                      ),
                      const SizedBox(height: 12),
                    ],
                    
                    if (virtualContent!['fileSize'] != null) ...[
                      _buildInfoRow('文件大小', virtualContent!['fileSize']),
                      const SizedBox(height: 12),
                    ],
                    
                    _buildInfoRow(
                      '有效期',
                      virtualContent!['validityDays'] == -1
                          ? '永久有效'
                          : '${virtualContent!['validityDays']}天',
                    ),
                  ],
                ),
              ),
              const SizedBox(height: 16),
            ],
            
            // 操作按钮
            Padding(
              padding: const EdgeInsets.all(16),
              child: Column(
                children: [
                  SizedBox(
                    width: double.infinity,
                    child: ElevatedButton(
                      onPressed: () => Navigator.pop(context, {'success': true}),
                      style: ElevatedButton.styleFrom(
                        backgroundColor: Colors.blue,
                        padding: const EdgeInsets.symmetric(vertical: 14),
                        shape: RoundedRectangleBorder(
                          borderRadius: BorderRadius.circular(8),
                        ),
                      ),
                      child: const Text(
                        '完成',
                        style: TextStyle(
                          fontSize: 16,
                          color: Colors.white,
                          fontWeight: FontWeight.w600,
                        ),
                      ),
                    ),
                  ),
                ],
              ),
            ),
          ],
        ),
      ),
    );
  }

  Widget _buildInfoRow(String label, String value, {VoidCallback? onCopy}) {
    return Row(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        SizedBox(
          width: 80,
          child: Text(
            label,
            style: const TextStyle(
              fontSize: 14,
              color: Colors.grey,
            ),
          ),
        ),
        Expanded(
          child: Text(
            value,
            style: const TextStyle(
              fontSize: 14,
              fontWeight: FontWeight.w500,
            ),
          ),
        ),
        if (onCopy != null)
          IconButton(
            icon: const Icon(Icons.copy, size: 18),
            onPressed: onCopy,
            padding: EdgeInsets.zero,
            constraints: const BoxConstraints(),
          ),
      ],
    );
  }

  void _copyToClipboard(BuildContext context, String text) {
    Clipboard.setData(ClipboardData(text: text));
    ScaffoldMessenger.of(context).showSnackBar(
      const SnackBar(
        content: Text('已复制到剪贴板'),
        duration: Duration(seconds: 1),
      ),
    );
  }
}
